package com.mzba.easy.weibo.db;

import java.util.ArrayList;
import java.util.List;

import com.mzba.easy.weibo.util.StringUtil;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * @author 06peng
 *
 */
public class EmotionTable {

	public static final String TABLE_NAME = "emotion_tb";
	public static final String ID = "_id";
	public static final String CATEGORY = "category";
	public static final String ICON = "icon";
	public static final String PHRASE = "phrase";
	public static final String TYPE = "type";
	
	private DBManagerImpl db = null;
	
	public EmotionTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_NAME)) {
			createTable();
		}
	}
	
	private void createTable() {
		String createSql = "create table if not exists " + TABLE_NAME + " ("
				+ ID + " int primary key," + CATEGORY + " varchar, "+ICON+" varchar, "
				+ PHRASE + " varchar," + TYPE + " varchar)";
		db.creatTable(db.getConnection(), createSql, TABLE_NAME);
	}
	
	public void save(EmotionEntry entry) {
		db.delete(db.getConnection(), TABLE_NAME, null, null);
		ContentValues values = new ContentValues();
		values.put(CATEGORY, entry.getCategory());
		values.put(ICON, entry.getIcon());
		values.put(PHRASE, entry.getPhrase());
		values.put(TYPE, entry.getType());
		db.save(db.getConnection(), TABLE_NAME, values);
	}
	
	public void saveLocalEmotions() {
		List<EmotionEntry> entries = new ArrayList<EmotionEntry>();
		entries.add(new EmotionEntry("shenshou_thumb", "[草泥马]"));
		entries.add(new EmotionEntry("horse2_thumb", "[神马]"));
		entries.add(new EmotionEntry("fuyun_thumb", "[浮云]"));
		entries.add(new EmotionEntry("geili_thumb", "[给力]"));
		entries.add(new EmotionEntry("wg_thumb", "[围观]"));
		entries.add(new EmotionEntry("vw_thumb", "[威武]"));
		entries.add(new EmotionEntry("panda_thumb", "[熊猫]"));
		entries.add(new EmotionEntry("rabbit_thumb", "[兔子]"));
		entries.add(new EmotionEntry("otm_thumb", "[奥特曼]"));
		entries.add(new EmotionEntry("j_thumb", "[囧]"));
		entries.add(new EmotionEntry("hufen_thumb", "[互粉]"));
		entries.add(new EmotionEntry("liwu_thumb", "[礼物]"));
		entries.add(new EmotionEntry("smilea_thumb", "[呵呵]"));
		entries.add(new EmotionEntry("tootha_thumb", "[嘻嘻]"));
		entries.add(new EmotionEntry("laugh", "[哈哈]"));
		entries.add(new EmotionEntry("tza_thumb", "[可爱]"));
		entries.add(new EmotionEntry("kl_thumb", "[可怜]"));
		entries.add(new EmotionEntry("kbsa_thumb", "[挖鼻屎]"));
		entries.add(new EmotionEntry("cj_thumb", "[吃惊]"));
		entries.add(new EmotionEntry("shamea_thumb", "[害羞]"));
		entries.add(new EmotionEntry("zy_thumb", "[挤眼]"));
		entries.add(new EmotionEntry("bz_thumb", "[闭嘴]"));
		entries.add(new EmotionEntry("bs2_thumb", "[鄙视]"));
		entries.add(new EmotionEntry("lovea_thumb", "[爱你]"));
		entries.add(new EmotionEntry("sada_thumb", "[泪]"));
		entries.add(new EmotionEntry("heia_thumb", "[偷笑]"));
		entries.add(new EmotionEntry("qq_thumb", "[亲亲]"));
		entries.add(new EmotionEntry("sb_thumb", "[生病]"));
		entries.add(new EmotionEntry("mb_thumb", "[太开心]"));
		entries.add(new EmotionEntry("ldln_thumb", "[赖的理你]"));
		entries.add(new EmotionEntry("yhh_thumb", "[右哼哼]"));
		entries.add(new EmotionEntry("zhh_thumb", "[左哼哼]"));
		entries.add(new EmotionEntry("x_thumb", "[嘘]"));
		entries.add(new EmotionEntry("cry", "[衰]"));
		entries.add(new EmotionEntry("wq_thumb", "[委屈]"));
		entries.add(new EmotionEntry("t_thumb", "[吐]"));
		entries.add(new EmotionEntry("k_thumb", "[打哈欠]"));
		entries.add(new EmotionEntry("bba_thumb", "[抱抱]"));
		entries.add(new EmotionEntry("angrya_thumb", "[怒]"));
		entries.add(new EmotionEntry("yw_thumb", "[疑问]"));
		entries.add(new EmotionEntry("cza_thumb", "[馋嘴]"));
		entries.add(new EmotionEntry("88_thumb", "[拜拜]"));
		entries.add(new EmotionEntry("sk_thumb", "[思考]"));
		entries.add(new EmotionEntry("sweata_thumb", "[汗]"));
		entries.add(new EmotionEntry("sleepya_thumb", "[困]"));
		entries.add(new EmotionEntry("sleepa_thumb", "[睡觉]"));
		entries.add(new EmotionEntry("money_thumb", "[钱]"));
		entries.add(new EmotionEntry("sw_thumb", "[失望]"));
		entries.add(new EmotionEntry("cool_thumb", "[酷]"));
		entries.add(new EmotionEntry("hsa_thumb", "[花心]"));
		entries.add(new EmotionEntry("hatea_thumb", "[哼]"));
		entries.add(new EmotionEntry("gza_thumb", "[鼓掌]"));
		entries.add(new EmotionEntry("dizzya_thumb", "[晕]"));
		entries.add(new EmotionEntry("bs_thumb", "[悲伤]"));
		entries.add(new EmotionEntry("crazya_thumb", "[抓狂]"));
		entries.add(new EmotionEntry("h_thumb", "[黑线]"));
		entries.add(new EmotionEntry("yx_thumb", "[阴险]"));
		entries.add(new EmotionEntry("nm_thumb", "[怒骂]"));
		entries.add(new EmotionEntry("hearta_thumb", "[心]"));
		entries.add(new EmotionEntry("unheart", "[伤心]"));
		entries.add(new EmotionEntry("pig", "[猪头]"));
		entries.add(new EmotionEntry("ok_thumb", "[ok]"));
		entries.add(new EmotionEntry("ye_thumb", "[耶]"));
		entries.add(new EmotionEntry("good_thumb", "[good]"));
		entries.add(new EmotionEntry("no_thumb", "[不要]"));
		entries.add(new EmotionEntry("z2_thumb", "[赞]"));
		entries.add(new EmotionEntry("come_thumb", "[来]"));
		entries.add(new EmotionEntry("sad_thumb", "[弱]"));
		entries.add(new EmotionEntry("lazu_thumb", "[蜡烛]"));
		entries.add(new EmotionEntry("cake", "[蛋糕]"));
		entries.add(new EmotionEntry("clock_thumb", "[钟]"));
		entries.add(new EmotionEntry("m_thumb", "[话筒]"));
		entries.add(new EmotionEntry("flag_thumb", "[国旗]"));
		entries.add(new EmotionEntry("lxhblowakiss_thumb", "[飞个吻]"));
		entries.add(new EmotionEntry("gangnamstyle_thumb", "[江南style]"));
		entries.add(new EmotionEntry("lxhwahaha_thumb", "[笑哈哈]"));
		entries.add(new EmotionEntry("lxhtongku_thumb", "[泪流满面]"));
		entries.add(new EmotionEntry("lxhdaigan_thumb", "[带感]"));
		entries.add(new EmotionEntry("lxhdese_thumb", "[得瑟]"));
		entries.add(new EmotionEntry("gstnaini_thumb", "[gst耐你]"));
		entries.add(new EmotionEntry("gsthaoxiushe_thumb", "[gst好羞涩]"));
		entries.add(new EmotionEntry("xbxiaohua_thumb", "[xb小花]"));
		entries.add(new EmotionEntry("xbyali_thumb", "[xb压力]"));
		entries.add(new EmotionEntry("dintuizhuang_thumb", "[din推撞]"));
		entries.add(new EmotionEntry("brdlingluan_thumb", "[bed凌乱]"));
		entries.add(new EmotionEntry("lxhyueeryuan_thumb", "[月儿圆]"));
		entries.add(new EmotionEntry("lxhfangjiale_thumb", "[放假啦]"));
		entries.add(new EmotionEntry("lxhzuiyou_thumb", "[最右]"));
		entries.add(new EmotionEntry("lxhliangle_thumb", "[右边亮了]"));
		entries.add(new EmotionEntry("lxhzhuanfa_thumb", "[转发]"));

		List<ContentValues> valueList = new ArrayList<ContentValues>();
		for (EmotionEntry entry : entries) {
			if (StringUtil.isBlank(entry.getCategory())) {
				ContentValues values = new ContentValues();
				values.put(ICON, entry.getIcon());
				values.put(PHRASE, entry.getPhrase());
				valueList.add(values);
			}
		}
		db.saveAll(db.getConnection(), TABLE_NAME, valueList);
	}
	
	
	public void saveAll(List<EmotionEntry> entries) {
		List<ContentValues> valueList = new ArrayList<ContentValues>();
		for (EmotionEntry entry : entries) {
			if (StringUtil.isBlank(entry.getCategory())) {
				ContentValues values = new ContentValues();
				values.put(CATEGORY, entry.getCategory());
				values.put(ICON, entry.getIcon());
				values.put(PHRASE, entry.getPhrase());
				values.put(TYPE, entry.getType());
				valueList.add(values);
			}
		}
		db.saveAll(db.getConnection(), TABLE_NAME, valueList);
	}
	
	public List<EmotionEntry> getAllEmotions() {
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME, null);
		if (cursor == null)
			return null;
		List<EmotionEntry> entries = new ArrayList<EmotionEntry>();
		EmotionEntry entry = null;
		try {
			while (cursor.moveToNext()) {
				entry = new EmotionEntry();
				entry.setCategory(cursor.getString(cursor.getColumnIndex(CATEGORY)));
				entry.setIcon(cursor.getString(cursor.getColumnIndex(ICON)));
				entry.setPhrase(cursor.getString(cursor.getColumnIndex(PHRASE)));
				entry.setType(cursor.getString(cursor.getColumnIndex(TYPE)));
				entries.add(entry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return entries;
	}
	
	public List<EmotionEntry> getEmotionsByType(String type) {
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME + " where "+TYPE+" = ?", new String[]{type});
		if (cursor == null)
			return null;
		List<EmotionEntry> entries = new ArrayList<EmotionEntry>();
		EmotionEntry entry = null;
		try {
			while (cursor.moveToNext()) {
				entry = new EmotionEntry();
				entry.setCategory(cursor.getString(cursor.getColumnIndex(CATEGORY)));
				entry.setIcon(cursor.getString(cursor.getColumnIndex(ICON)));
				entry.setPhrase(cursor.getString(cursor.getColumnIndex(PHRASE)));
				entry.setType(cursor.getString(cursor.getColumnIndex(TYPE)));
				entries.add(entry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return entries;
	}
}
